package com.liangdemo.web.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liangdemo.web.Service.UserService;
import com.liangdemo.web.export.Test1;
import com.liangdemo.web.export.Tet;
import com.liangdemo.web.model.Account;
import com.liangdemo.web.model.User;
import com.liangdemo.web.paging.PaginationUtil;
import com.liangdemo.web.utils.ajax.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/user")
@ResponseBody
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 获取application.yml中的配置信息
     */
    @Value("${group.default.value}")
    private String value;


    @GetMapping("/value")
    public String get() {
        return value;
    }

    /**
     * 分页查询全部用户
     *
     * @param pageable
     * @return
     */
    @GetMapping("/find")
    public AjaxResult page(Pageable pageable) {
        Page<User> page = PaginationUtil.getPage(pageable);
        Page<User> page1 = userService.page(page);
        return AjaxResult.success(page1);

    }

    /**
     * 查询一个用户
     */
    @GetMapping("/findAll")
    public AjaxResult findAll(String name) {
        return AjaxResult.success(userService.list(new LambdaQueryWrapper<User>().like(User::getUserName, name)));
    }

    /**
     * 分页查询全部用户
     */
    @GetMapping("/list")
    public AjaxResult list(HttpServletResponse response, String fileName) {
        fileName = "ceshi";
        List<User> userList = userService.list();
        // ExcelUtils.exportExcel(userList, null, "测试EXCEL1", User.class, fileName + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()) + ".xlsx", response);


        return AjaxResult.success(userList);
    }


    /**
     * 查询用户详细信息
     * 表的联合查询
     *
     * @return
     */
    @GetMapping("/Detailed")
    public AjaxResult detailed(String userid) {

//        ExecutorService es = Executors.newFixedThreadPool(200);
//        for (int i = 0; i < 500; i++) {
//            es.submit(new Runnable() {
//                @Override
//                public void run() {
//                    System.out.println("多线程");
//                    userService.Detailed(userid);
//                }
//            });
//        }


        return AjaxResult.success(userService.Detailed(userid));
    }

    /**
     * 删除一个用户
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public AjaxResult delete(Integer id) {
        System.out.println(id);
        userService.list();
        return AjaxResult.success(userService.removeById(id));
    }


    /**
     * 用户信息的修改
     */
    @PutMapping("/put")
    public AjaxResult put(User user) {
        return AjaxResult.success(userService.update(user, new LambdaQueryWrapper<User>().eq(User::getId, user.getId())));
    }

    @GetMapping("/daochu")
    public ResponseEntity<byte[]> test(HttpServletResponse response) {
        List<Tet> tets = new ArrayList<>();

        tets.add(new Tet("测试1", "测试2", "测试三", "cesh"));
        tets.add(new Tet("测试1", "测试2", "测试三", "cesh"));
        tets.add(new Tet("测试1", "测试2", "测试三", "cesh"));

        return Test1.emp2Excel(tets, response);
    }

    @PostMapping("/add")

    public void ceshi(User user, Account account) {
        System.out.println(user.getUserName());
        System.out.println(account.getUserId());

    }
}
